$.cookie('isMini', '', {path: '/'})

var tabIndex = 0;
$(".monTab").on("click","li",function(){
    tabIndex = $(this).index()
    $('#phoneNumber').val('')
    $('#loginPhone').val('')
    $('#loginPwd').val('')
    $('#verificationCode').val('')
    if(tabIndex == 1){
        $('.forget').show()
    }else{
        $('.forget').hide()
    }
    $(this).addClass("active").siblings().removeClass("active");
    $(".loginBox > div").eq($(this).index()).show().siblings().hide();
})
// 倒计时30s
var timestart = 30;
var timestep = -1;
var timeID;
function timecount() {
    document.getElementById("timeout").innerHTML = timestart;
    timestart += timestep;
    if(timestart < 0){
      timestart = 30;
      //倒计时为0时执行xxxx方法   
      $("#send").show()
      $("#daoji").hide()
    }
    timeID=setTimeout("timecount()",1000);
}

// 验证码
// var maCode;
function sendYz(){
    var phone = $.trim($('#phoneNumber').val())
   
     var isRegister = true
    if(!phone){
        // alert("请输入手机号码")
        layer.open({
            content: "请输入手机号码",
            btn: '确定'
          });
       return
     }
  
    
    //  getCode({phone, typeCode, isRegister}).then(res => {
    //     layer.open({
    //         content: "验证码发送成功",
    //         btn: '确定'
    //       });
    //  }).catch(res => {
    //     layer.open({
    //         content: res.message,
    //         btn: '确定'
    //       });
    //     $("#send").show()
    //     $("#daoji").hide()
    // })



    loadding = layer.open({type: 2});
    getGeetest({
        loginname:phone, 
        mark:'mobilelogin'
    }).then(function(){
        layer.close(loadding)
        return getSmsCode({
            phone: phone,
            typeCode: 'MobileLogin',
            mark: 'mobilelogin'
        })
    }).then(function(res){
        layer.open({
            content: '手机验证码已发送'
            ,skin: 'msg'
            ,time: 2
        });
        $("#send").hide()
        $("#daoji").show()
         timecount()
    })
 }

//  登录
function beforeSubmit(){
    window.scrollTo(0, 0)
    if(!$('#paixu1').is(':checked')) {
        layer.open({
            content: '我同意若未注册用户，登录即注册账号',
            btn: ['确定','取消'],
            yes: function(index, layero){
                layer.close(index)
                submitLogin()
            }
          });
    }else{
        submitLogin()
    }
}
function submitLogin() {
    var layerIndex = layer.open({type: 2, shadeClose: false});
    var LoginName = tabIndex == 0 ? $.trim($('#phoneNumber').val()) : $('#loginPhone').val()
    var LoginPwd = $.trim($('#loginPwd').val())
    var verificationCode = $.trim($('#verificationCode').val())
    var OpenId = $.cookie('OpenId')
    var UnionId = $.cookie('unionID')
    var IsSms = tabIndex == 0 ? true : false
    var SmsCode = tabIndex == 0 ? verificationCode : ''
    var InviteUserId = localStorage.getItem("inviteUserId") ? localStorage.getItem("inviteUserId") : ''
    var AppId = getParams().appid
    if (!AppId) {
        AppId = appid
    }
    // if (!InviteUserId) {
    //     let nui = getParams().inviteUserId
    //     if (nui) {
    //         InviteUserId = nui
    //     }
    // }
    userLogin({LoginName, LoginPwd, OpenId, UnionId, IsSms, SmsCode, InviteUserId, AppId}).then(res => {
        layer.close(layerIndex);
        $.cookie('loginName', LoginName, { expires: 100, path: '/', domain: 'xueerqin.net' })
        $.cookie('loginUserId', res.loginUserId, { expires: 100, path: '/', domain: 'xueerqin.net' })
        $.cookie('authToken', JSON.stringify(res.oAuth), {
            path: '/',
            domain: '.xueerqin.net',
            expires: 100
        })
       var url = location.origin + location.pathname
        if (res.userRoles.length == 1) {
            $.cookie('isMoreRole', false, { expires: 100, path: '/', domain: 'xueerqin.net' })
            $.cookie('roleCode', res.userRoles[0].code, { expires: 100, path: '/', domain: 'xueerqin.net' })
        } else if (res.userRoles.length == 2) {
            $.cookie('isMoreRole', true, { expires: 100, path: '/', domain: 'xueerqin.net' })
        } else if (res.userRoles.length == 0) {

        }
        // ==============================================================
    //    是否需要修改密码
       if(res.isNeedSetPwd){
            location.href = url.replace('static/signUp.html', 'index.html?v='+ Math.random() +'#/signUp/setPassWord')
            return
       }
    //    根据role判断扫码进来的用户
    // 添加孩子
       if(localStorage.getItem("role") == 'parent'){
            if(localStorage.getItem("localQRCode")){
                location.href = url.replace('static/signUp.html', 'index.html?v='+ Math.random() +'#/myNew/addChildren')
            }
            return
       }
    // 老师
       if(localStorage.getItem("role") == 'teacher'){
           if(localStorage.getItem("localQRCode")){
            //   加入班级
            location.href = url.replace('static/signUp.html', 'index.html?v='+ Math.random() +'#/myNew/joinClass')
           }else{
               //    创建班级
               location.href = url.replace('static/signUp.html', 'index.html?v='+ Math.random() +'#/myNew/establish')
           }
            return
        }

         // // 只有一个角色
       if(res.userRoles.length == 1){
            location.href = url.replace('static/signUp.html', 'index.html?v='+ Math.random() +'#/menu/index')
            return
        }
        //    需要选择角色
        if(res.userRoles.length == 2){
            location.href = url.replace('static/signUp.html', 'index.html?v='+ Math.random() +'#/signUp/choice')
            return
        }
        // 选择创建的身份
    //   location.href = url.replace('static/signUp.html', 'index.html?v='+ Math.random() +'#/myNew/children')
       location.href = url.replace('static/signUp.html', 'index.html?v='+ Math.random() +'#/signUp/selectChoice')
    }).catch(res => {
        layer.close(layerIndex);
        layer.open({
            content: res.message,
            btn: '确定'
          });
        // alert(res.message)
    })
}

function start(){
    var layerIndex = layer.open({type: 2, shadeClose: false});
    var openid = $.cookie('OpenId')
    var unionid = $.cookie('unionID')
    userWechatInfoForComponent().then(res => {
        
        openid = res.openId
        unionid = res.unionId
        return refreshToken()
       
    }).then(res => {
        
        return getUserInfo({openid, unionid})
    }).then(res => {
        layer.close(layerIndex);
        $.cookie("loginName", res.loginName, { expires: 100, path: '/', domain: 'xueerqin.net' });
        $.cookie("cellPhoneNum", res.loginName, { expires: 100, path: '/', domain: 'xueerqin.net' });
        
        var url = location.origin + location.pathname
        var roleCount = res.roleCount
        var roles = res.roles
        if(localStorage.getItem("role") == 'parent'){                    
            if(localStorage.getItem("localQRCode")){
                //添加孩子
                location.href = url.replace('static/signUp.html', 'index.html?v='+ Math.random() +'#/myNew/addChildren')
                return
            } else {
                location.href = url.replace('static/signUp.html', 'index.html?v='+ Math.random() +'#/myNew/children') 
                return
            }
        } else if(localStorage.getItem("role") == 'teacher'){
            if(localStorage.getItem("localQRCode")){
                //   加入班级
                location.href = url.replace('static/signUp.html', 'index.html?v='+ Math.random() +'#/myNew/joinClass')
                return
            }else{
                //    创建班级
                location.href = url.replace('static/signUp.html', 'index.html?v='+ Math.random() +'#/myNew/establish')
                return
            }
        }


        if (roleCount > 1) {
            
            $.cookie('isMoreRole', true, { expires: 100, path: '/', domain: 'xueerqin.net' })
            location.href = url.replace('static/signUp.html', 'index.html?v='+ Math.random() +'#/signUp/choice')
        }
        else if (roleCount == 1) {
            $.cookie('isMoreRole', false, { expires: 100, path: '/', domain: 'xueerqin.net' })
            $.cookie('roleCode', roles[0], { expires: 100, path: '/', domain: 'xueerqin.net' })
            location.href = url.replace('static/signUp.html', 'index.html?v='+ Math.random() +'#/menu/index')
            // 
            
        } else if (roleCount == 0) {
            
            // exit(openid)
            mobileIndexCheck().then(data3 => {
                if(data3.isNeedSetPwd){
                  location.href = url.replace('static/signUp.html', 'index.html?v='+ Math.random() +'#/signUp/setPassWord')
                  return
                }
                // if(localStorage.getItem("role") == 'parent'){                    
                //     if(localStorage.getItem("localQRCode")){
                //         //添加孩子
                //         location.href = url.replace('static/signUp.html', 'index.html?v='+ Math.random() +'#/myNew/addChildren')
                //         return
                //     }
                //     location.href = url.replace('static/signUp.html', 'index.html?v='+ Math.random() +'#/myNew/children') 
                // } else if(localStorage.getItem("role") == 'teacher'){
                //     if(localStorage.getItem("localQRCode")){
                //         //   加入班级
                //         location.href = url.replace('static/signUp.html', 'index.html?v='+ Math.random() +'#/myNew/joinClass')
                //     }else{
                //         //    创建班级
                //         location.href = url.replace('static/signUp.html', 'index.html?v='+ Math.random() +'#/myNew/establish')
                //     }
                // } else {
                    location.href = url.replace('static/signUp.html', 'index.html?v='+ Math.random() +'#/signUp/selectChoice')
                // }
              })
        }
       
    }).catch(err => {
        layer.close(layerIndex);
        var res = JSON.parse(err.responseText).error
        if (res.code != 1) {
            layer.open({
                content: res.message,
                btn: '确定'
              });
            // alert(res.message)
        }
        // layer.close(layerIndex);
    })

}


start()
 

